package group

import (
	"common/mildware"
	"common/models"
	"common/validate"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"project/docModel"
	"strconv"
	"strings"
)

func (h GHandler) CreateGroup(c *gin.Context) {
	resp := new(models.RespData)
	req := new(docModel.CreateGroupReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Group:CreateGroup with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Group:CreateGroup with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//token
	ID, _, nickName, err := mildware.GetCyrrentUserID(c)
	if err != nil {
		resp.SendServerBusy(c)
		return
	}
	//查看本人群聊的数量
	groupNameMap := make(map[string]struct{})
	groups, count, err := h.M.GroupAllGroupInfoByUserID(ID)
	if err != nil {
		zap.L().Error("Group:CreateGroup  h.M.GroupAllGroupInfoByUserID", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if count > 5 {
		resp.SendCode(c, models.MaxUserRole)
		return
	}
	for _, v := range groups {
		groupNameMap[v.Name] = struct{}{}
	}
	if _, ok := groupNameMap[req.Name]; ok {
		resp.SendCode(c, models.CodeUserExist)
		return
	}
	group := &models.GroupInfo{
		Name:      req.Name,
		OwnerId:   ID,
		OwnerName: nickName,
		Text:      req.Text,
		Peoples:   ID + "|" + req.Peoples,
		ImgSrc:    req.ImgSrc,
		ImgId:     req.ImgId,
	}
	err = h.M.GroupCreate(group)
	if err != nil {
		zap.L().Error("Group:CreateGroup  h.M.GroupCreate", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	GroupInfo, err := h.M.GroupSearchByName(req.Name)
	if err != nil {
		zap.L().Error("Group:CreateGroup  h.M.GroupSearchByName", zap.Error(err))
		return
	}
	groupIDStr := strconv.Itoa(int(GroupInfo.ID))
	//变化群组的groups
	h.changeUserInfoToGroup(ID, groupIDStr)
	//变化组员的groups
	PeoplesList := strings.Split(req.Peoples, "|")
	for _, v := range PeoplesList {
		if v == "" {
			continue
		}
		//查找修改
		h.changeUserInfoToGroup(v, groupIDStr)
	}
	resp.SendSuccess(c)
}
func (h GHandler) changeUserInfoToGroup(ID string, groupID string) (err error) {
	exist, userInfo, err := h.M.UserCheckId(ID)
	if err != nil {
		zap.L().Error("Group:CreateGroup   h.M.UserCheckId", zap.Error(err))
		return
	}
	if exist {
		groups := strings.Split(userInfo.Groups, "|")
		userInfo.Groups = models.AddListToString(groups, groupID)
		fmt.Printf("%+v", userInfo)
		//更新用户
		err = h.M.UserUpdateById(userInfo)
		if err != nil {
			zap.L().Error("Group:CreateGroup  h.M.UserUpdateById", zap.Error(err))
			return
		}
	}
	return
}

func (h GHandler) AddPeopleToGroup(c *gin.Context) {
	resp := new(models.RespData)
	req := new(docModel.AddPeopleToGroupReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Group:AddPeopleToGroup with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Group:AddPeopleToGroup with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//获得用户的信息
	exist, info, err := h.M.UserCheckId(req.UserID)
	if err != nil {
		zap.L().Error("Group:AddPeopleToGroup  h.M.UserCheckId", zap.Error(err))
		return
	}
	if exist {
		groups := strings.Split(info.Groups, "|")
		info.Groups = models.AddListToString(groups, req.GroupID)
		//更新用户信息
		//更新用户
		err = h.M.UserUpdateById(info)
		if err != nil {
			zap.L().Error("Group:AddPeopleToGroup  h.M.UserUpdateById", zap.Error(err))
			return
		}
	}
	//获得群聊信息
	gInfo, err := h.M.GroupSelectByID(req.GroupID)
	if err != nil {
		zap.L().Error("Group:AddPeopleToGroup  h.M.GroupSelectByID", zap.Error(err))
		return
	}
	peoples := strings.Split(gInfo.Peoples, "|")
	gInfo.Peoples = models.AddListToString(peoples, req.UserID)
	//更新群
	err = h.M.GroupSaveByID(gInfo)
	if err != nil {
		zap.L().Error("Group:AddPeopleToGroup  h.M.GroupSaveByID", zap.Error(err))
		return
	}
	resp.SendSuccess(c)
}
